// pages/reportStatistics/reportStatistics.js
import { queryRecommendStatistics, queryCompanyRecommendStatistics } from "../../../../service/manager_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    companyList: [

    ],
    timeSelectIndex: 0,
    startTimeStr: "2021-09-08",
    endTimeStr: "2021-09-08",
    showTimeText: "",
    showYearPop: false,//年弹窗
    showMonthPop: false,//月弹窗
    showDayPop: false,//天弹窗
    maxDate: new Date(2025, 12, 31).getTime(),
    bigMaxDate: new Date(2050, 12, 31).getTime(),
    minDate: new Date(2021, 8, 1).getTime(),
    infoData: {},
    currentType: 1,//1: 公司  2: 个人 3: 渠道
    delayCalendarShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.startTimeInit();
    setTimeout(() => {
      this.setData({
        delayCalendarShow: true
      })
    }, 1500);
  },
  //初始数据:
  startTimeInit() {
    let d = new Date();
    let str = d.getFullYear().toString() + "-" + this.addZero(d.getMonth() + 1) + "-" + this.addZero(d.getDate())
    console.log(str);
    let startTime = str + " " + "00:00:00";
    let endTime = str + " " + "23:59:59";
    this.setData({
      startTimeStr: startTime,
      endTimeStr: endTime,
      showTimeText: str
    })
    this.queryCompanyRecommendStatistics();
  },
  addZero(v) {
    if (v < 10)
      return '0' + v;
    return v.toString();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  topChangeTypeAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentType: index
    })
    this.queryCompanyRecommendStatistics();
  },
  //查询信息
  queryCompanyRecommendStatistics() {
    wx.showLoading();
    queryCompanyRecommendStatistics(this.data.startTimeStr, this.data.endTimeStr, this.data.currentType).then(res => {
      wx.hideLoading();
      console.log("JJLog:统计信息:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          infoData: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryCompanyRecommendStatistics", err);
    })
  },
  //顶部切换时间:
  timeChangeAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      timeSelectIndex: index,
    })
    this.selectTimeAction();
  },
  //选择时间:
  selectTimeAction() {
    if (this.data.timeSelectIndex == 0) {
      this.setData({
        showDayPop: true
      })
    } else if (this.data.timeSelectIndex == 1) {
      //月
      this.setData({
        showMonthPop: true
      })
    } else {
      //年:
      this.setData({
        showYearPop: true
      })
    }
  },

  //选年回调:
  yearTimeSelect(e) {
    console.log("JJLog:年回调:" + JSON.stringify(e));
    let detail = e.detail.result;
    let startTime = detail.value + " " + "00:00:00";
    let showStartTime = detail.value;
    let endTimeArr = detail.value.split("-");
    let endTime = endTimeArr[0] + "-" + "12-31" + " " + "23:59:59"
    let showEndTime = endTimeArr[0] + "-" + "12-31";
    this.setData({
      startTimeStr: startTime,
      endTimeStr: endTime,
      showTimeText: showStartTime + " - " + showEndTime,
      showYearPop: false
    })
    this.queryCompanyRecommendStatistics();
  },
  //月回调:
  monthTimeSelect(e) {
    console.log("JJLog:月回调:" + JSON.stringify(e));
    let detail = e.detail.timeStr;
    let { monthNumber, dayNumber, timeStr } = detail;
    let startArr = timeStr.split("-");
    let startTime = startArr[0] + "-" + startArr[1] + "-01" + " " + "00:00:00";
    let showStartTime = startArr[0] + "-" + startArr[1] + "-01";
    let endTimeArr = timeStr.split("-");
    let endTime = "";
    let lastDay = this.dealLastMonthDay(monthNumber);
    endTime = endTimeArr[0] + "-" + endTimeArr[1] + "-" + lastDay + " " + "23:59:59"
    let showEndTime = endTimeArr[0] + "-" + endTimeArr[1] + "-" + lastDay;
    this.setData({
      startTimeStr: startTime,
      endTimeStr: endTime,
      showTimeText: showStartTime + " - " + showEndTime,
      showMonthPop: false
    })
    this.queryCompanyRecommendStatistics();
  },
  //处理一个月的最后一天:
  dealLastMonthDay(num) {
    let returnDay = 0;
    switch (num) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        returnDay = 31
        break;
      case 4:
      case 6:
      case 9:
      case 11:
        returnDay = 30
        break;
      case 2:
        returnDay = 28
        break;

      default:
        break;
    }
    return returnDay;
  },
  //天确认选择:
  onConfirmCal(e) {
    console.log("JJLog:e:" + JSON.stringify(e));
    let timeArr = e.detail;
    this.timeDealMethod(timeArr);
  },
  //时间处理方法
  timeDealMethod(timeArr) {
    let startDate = timeArr[0];
    let endDate = timeArr[1];
    let startTime = "";
    let endTime = "";
    let startMonth = (startDate.getMonth() + 1) > 9 ? (startDate.getMonth() + 1) : ("0" + (startDate.getMonth() + 1));
    let startDay = (startDate.getDate() + 1) > 9 ? (startDate.getDate()) : ("0" + startDate.getDate());
    let endMonth = (endDate.getMonth() + 1) > 9 ? (endDate.getMonth() + 1) : ("0" + (endDate.getMonth() + 1));
    let endDay = (endDate.getDate() + 1) > 9 ? (endDate.getDate()) : ("0" + endDate.getDate());
    startTime = startDate.getFullYear() + '-' + startMonth + '-' + startDay;
    endTime = endDate.getFullYear() + '-' + endMonth + '-' + endDay;
    this.setData({
      showDayPop: false,
      startTimeStr: startTime + " " + "00:00:00",
      endTimeStr: endTime + " " + "23:59:59"
    })
    if (startTime == endTime) {
      this.setData({
        showTimeText: startTime
      })
    } else {
      this.setData({
        showTimeText: startTime + " - " + endTime,
      })
    }
    this.queryCompanyRecommendStatistics();

    // console.log("JJLog:开始时间:" + startTime);
    // console.log("JJLog:结束时间:" + endTime);
  },



  //选年弹窗
  timePopHidden() {
    this.setData({
      showMonthPop: false,
    })
  },
  singlePopHide() {
    this.setData({
      showYearPop: false,
    })
  },
  onCloseCal() {
    this.setData({
      showDayPop: false,
    })
  },


})